package losrebellos.lospistoleros.components.application.movieclip 
{
	import losrebellos.lospistoleros.components.application.IAbstractPApplication;
	import losrebellos.lospistoleros.components.application.IAbstractPApplicationController;
	import losrebellos.lospistoleros.components.application.IAbstractPApplicationModel;
	import losrebellos.lospistoleros.core.controller.movieclip.PControllerMovieClip;
	import losrebellos.lospistoleros.core.model.movieclip.IPModelMovieClip;
	import losrebellos.lospistoleros.core.view.IPView;
	import losrebellos.lospistoleros.core.view.movieclip.IPViewMovieClip;

	import flash.display.LoaderInfo;
	import flash.events.Event;

	/*
	 *
	 * @author los rebellos - ben@benoitvinay.com
	 *
	 */
	public class AbstractPApplicationControllerMovieClip extends PControllerMovieClip implements IAbstractPApplicationController
	{
		/*
		 *
		 * VARIABLES
		 *
		 */
		protected var _loaderInfo:LoaderInfo = null;
		protected var _parameters:Object = null;
		
		
		/*
		 *
		 * CONSTRUCTOR
		 *
		 */
		public function AbstractPApplicationControllerMovieClip(m:IPModelMovieClip, v:IPViewMovieClip = null, name:String = null, type:String = null)
		{
			super(m, v, name, type);
		}
		protected function get _abstractModel():IAbstractPApplicationModel
		{
			return _model as IAbstractPApplicationModel;
		}
		protected function get _abstractView():IAbstractPApplication
		{
			return _view as IAbstractPApplication;
		}
		
		
		/*
		 * 
		 * VIEW
		 * 
		 */
		override protected function saveView(v:IPView):void
		{
			super.saveView(v);
			
			_view.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			_view.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
		}
		
		
		/*
		 * 
		 * VIEW
		 * 
		 */
		protected function addedToStageHandler(e:Event):void
		{
			_view.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			
			initializeFlashVars();
			initializeStage();
			
			_abstractView.createApplicationContent();
			_abstractView.addApplicationContent();
			
			resizeHandler();
			
			initialize();
		}
		protected function removedFromStageHandler(e:Event):void
		{
			_view.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
			
			finalizeFlashVars();
			finalizeStage();
			
			finalize();
		}
		
		
		/*
		 * 
		 * FLASH VARS
		 * 
		 */
		protected function initializeFlashVars():void
		{
			_abstractModel.loaderInfo = _view.parent.loaderInfo;
		}
		protected function finalizeFlashVars():void
		{
			
		}
		
		
		/*
		 * 
		 * STAGE
		 * 
		 */
		protected function initializeStage():void
		{
			_view.stage.align = _abstractModel.stageVO.align;
			_view.stage.scaleMode = _abstractModel.stageVO.scaleMode;
			_view.stage.quality = _abstractModel.stageVO.quality;
			_view.stage.addEventListener(Event.RESIZE, resizeHandler);
		}
		protected function finalizeStage():void
		{
			_view.stage.removeEventListener(Event.RESIZE, resizeHandler);
		}
		// stage resize
		protected function resizeHandler(e:Event = null):void
		{
			_view.resize(_abstractModel.stageVO.rectangle.getRectangleFromStage(_view.stage));
		}
		//
	}
}